package workbench

import (
	"context"
	"github.com/gin-gonic/gin"
	etcd "github.com/rpcxio/rpcx-etcd/client"
	"github.com/smallnest/rpcx/client"
	"h_rpcx/client/basics"
	"h_rpcx/internet/service/wrokbenchService"
)

type WorkBench struct {
	workbenchClient client.XClient
}

func NewWorkbench() *WorkBench {
	discovery, err := etcd.NewEtcdV3Discovery(basics.EtcdS.BasePath, basics.EtcdS.WorkbenchPath, []string{basics.EtcdS.Addr}, false, nil)
	if err != nil {
		panic(err.Error())
	}
	xClient := client.NewXClient(basics.EtcdS.WorkbenchPath, client.Failtry, client.RandomSelect, discovery, client.DefaultOption)
	return &WorkBench{
		workbenchClient: xClient,
	}
}

//我的艺人
func (w *WorkBench) MyArtist(ctx *gin.Context) {
	resp := &wrokbenchService.MyArtistResp{}
	err := w.workbenchClient.Call(context.Background(), "MyArtist", &wrokbenchService.MyArtistReq{Uid: 1127}, resp)
	if err != nil {
		panic(err.Error())
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"data": resp,
	})
}

//列表
func (w *WorkBench) List(ctx *gin.Context) {
	key := ctx.Query("key")
	req := &wrokbenchService.ListReq{Key: key, PageSize: 10, Uid: 1127}
	resp := &wrokbenchService.ListResp{}
	err := w.workbenchClient.Call(context.Background(), "List", req, resp)
	if err != nil {
		panic(err.Error())
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"data": resp,
	})
}
